#include "log.h"
#include "message.hxx"
#include "mysql_message.h"

#include <iostream>

void test_insert(chat::MessageTable& message_table) {
    chat::Message message1(
        "message-1", "chat_session-1", "uid-1",
        boost::posix_time::time_from_string("2025-8-1 14:26:20.000"), 1);
    message_table.insert(message1);

    chat::Message message2(
        "message-2", "chat_session-1", "uid-2",
        boost::posix_time::time_from_string("2025-8-1 14:26:21.000"), 0);
    message_table.insert(message2);

    chat::Message message3(
        "message-3", "chat_session-2", "uid-1",
        boost::posix_time::time_from_string("2025-8-1 14:26:22.000"), 1);
    message_table.insert(message3);

    chat::Message message4(
        "message-4", "chat_session-2", "uid-2",
        boost::posix_time::time_from_string("2025-8-1 14:26:23.000"), 2);
    message_table.insert(message4);

    chat::Message message5(
        "message-5", "chat_session-2", "uid-1",
        boost::posix_time::time_from_string("2025-8-1 14:26:24.000"), 1);
    message_table.insert(message5);

    chat::Message message6(
        "message-6", "chat_session-2", "uid-2",
        boost::posix_time::time_from_string("2025-8-1 14:26:25.000"), 2);
    message_table.insert(message6);
}

void test_remove(chat::MessageTable& message_table) {
    message_table.remove("chat_session-1");
}

void test_recent(chat::MessageTable& message_table) {
    auto messages = message_table.recent("chat_session-2", 4);
    for (auto& message : messages) {
        std::cout << message.message_id() << std::endl;
        std::cout << boost::posix_time::to_simple_string(message.created_time())
                  << std::endl;
        std::cout << message.message_type() << std::endl;
    }
}

void test_range(chat::MessageTable& message_table) {
    auto messages = message_table.range(
        "chat_session-2",
        boost::posix_time::time_from_string("2025-8-1 14:26:19.000"),
        boost::posix_time::time_from_string("2025-8-1 14:26:25.000"));
    for (auto& message : messages) {
        std::cout << message.message_id() << std::endl;
        std::cout << boost::posix_time::to_simple_string(message.created_time())
                  << std::endl;
        std::cout << message.message_type() << std::endl;
    }
}

int main() {
    chat::initLogger(1, false, " ");

    std::shared_ptr<odb::mysql::database> db = chat::DataBaseFactory::create(
        1, "root", "123", "chat", "127.0.0.1", 0, "utf8");

    chat::MessageTable message_table(db);

    // test_insert(message_table)
    // test_remove(message_table);

    test_recent(message_table);
    std::cout << "---------" << std::endl;
    test_range(message_table);

    return 0;
}